package leetcode_字符串._07数字与字符串间转换;

import java.util.Arrays;
import java.util.HashMap;

/**
 * 相对名次
 */
public class _506 {
    public static void main(String[] args) {
        System.out.println(Arrays.toString(findRelativeRanks(new int[]{5, 4, 3, 2, 1})));
    }

    public static String[] findRelativeRanks(int[] score) {
        String[] ans = new String[score.length];
        final int l = score.length;
        int[] tem = new int[l];
        System.arraycopy(score, 0, tem, 0, l);
        Arrays.sort(tem);
        for (int i = 0; i < l; i++) {
            int search = Arrays.binarySearch(tem, score[i]);
            if (search == l - 1) {
                ans[i] = "Gold Medal";
                continue;
            }
            if (search == l - 2) {
                ans[i] = "Silver Medal";
                continue;
            }
            if (search == l - 3) {
                ans[i] = "Bronze Medal";
                continue;
            }
            ans[i] = l - search + "";
        }
        return ans;
    }

    public static String[] findRelativeRanks_two(int[] score) {
        String[] ans = new String[score.length];
        HashMap<Integer, Integer> map = new HashMap<>();
        for (int i = 0; i < score.length; i++) {
            map.put(score[i], i);
        }
        Arrays.sort(score);
        for (int i = score.length - 1; i >= 0; --i) {
            int index = score.length - i;
            switch (index) {
                case 1 : ans[map.get(score[i])] = "Gold Medal";break;
                case 2 : ans[map.get(score[i])] = "Silver Medal";break;
                case 3 : ans[map.get(score[i])] = "Bronze Medal";break;
                default : ans[map.get(score[i])] = index + "";
            }
        }
        return ans;
    }
}
